package com.java.thread.yield;

public class YieldThread extends Thread {

    public YieldThread(String name) {
        super(name);
    }

    @Override
    public void run() {
        for (int i = 1; i <= 500; i++) {
            System.out.println("" + this.getName() + "-----" + i);
            // 当i为400时，该线程就会把CPU时间让掉，让其他或者自己的线程执行（也就是谁先抢到谁执行）
            if (i == 400) {
                this.yield();
                System.out.println("----------------------------------");
            }
        }
    }

    public static void main(String[] args) {
        YieldThread yt1 = new YieldThread("张三");
        YieldThread yt2 = new YieldThread("李四");
        yt1.start();
        yt2.start();
    }

}
